/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package helper;

import java.lang.reflect.Array;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.xml.crypto.Data;
import model.*;

/**
 *
 * @author Administrator
 */
@ManagedBean(name = "vlmb")
@SessionScoped
public class validatorManagedBean {

    @EJB
    private ApplicantFacade applicantFacade;
    private int day;
    private int month;
    private int year;
    private Applicant applicant = new Applicant();

    public Applicant getA() {
        return applicant;
    }

    public void setA(Applicant applicant) {
        this.applicant = applicant;
    }
    private UIInput dayInput;
    private UIInput monthInput;
    private UIInput yearInput;

    // PROPERTY: day
    public int getDay() {
        return day;
    }

    public void setDay(int newValue) {
        day = newValue;
    }

    // PROPERTY: month
    public int getMonth() {
        return month;
    }

    public void setMonth(int newValue) {
        month = newValue;
    }

    // PROPERTY: year
    public int getYear() {
        return year;
    }

    public void setYear(int newValue) {
        year = newValue;
    }

    // PROPERTY: dayInput
    public UIInput getDayInput() {
        return dayInput;
    }

    public void setDayInput(UIInput newValue) {
        dayInput = newValue;
    }

    // PROPERTY: monthInput
    public UIInput getMonthInput() {
        return monthInput;
    }

    public void setMonthInput(UIInput newValue) {
        monthInput = newValue;
    }

    // PROPERTY: yearInput
    public UIInput getYearInput() {
        return yearInput;
    }

    public void setYearInput(UIInput newValue) {
        yearInput = newValue;
    }

    public void validateDate(FacesContext context, UIComponent component, Object value) {

        int d = ((Integer) dayInput.getLocalValue()).intValue();
        int m = ((Integer) monthInput.getLocalValue()).intValue();
        int y = ((Integer) value).intValue();

        if (!isValidDate(d, m, y)) {
            throw new ValidatorException(new FacesMessage("Invalid Date"));
        }

    }

    private static boolean isValidDate(int d, int m, int y) {
        return false;
    }

    public validatorManagedBean() {
    }

    public void validate(FacesContext facesContext,
            UIComponent uIComponent, Object object) throws ValidatorException {

        String enteredEmail = (String) object;
        //Set the email pattern string
        Pattern p = Pattern.compile("^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$");

        //Match the given string with the pattern
        Matcher m = p.matcher(enteredEmail);

        //Check whether match is found
        boolean matchFound = m.matches();

        if (!matchFound) {
            FacesMessage message = new FacesMessage();
            message.setDetail("Email not valid");
            message.setSummary("Email not valid");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message);
        }
    }

    public void validateYear(
            FacesContext context,
            UIComponent componentToValidate,
            Object value)
            throws ValidatorException {

        UIInput uiInput = (UIInput) componentToValidate;
        if (uiInput.isValid()) {
            int numberValue = (Integer) value;
            if (numberValue > 50) {
                FacesMessage message = new FacesMessage(
                        FacesMessage.SEVERITY_WARN,
                        "Years of experience is invalid",
                        "Years of experience is invalid");
                throw new ValidatorException(message);
            } else if (numberValue < 1 || numberValue > 11) {
                FacesMessage message = new FacesMessage(
                        FacesMessage.SEVERITY_WARN,
                        "Years of experience is invalid",
                        "Years of experience is invalid");
                throw new ValidatorException(message);
            }
        }
    }

    public void validateSalary(
            FacesContext context,
            UIComponent componentToValidate,
            Object value)
            throws ValidatorException {

        UIInput uiInput = (UIInput) componentToValidate;
        if (uiInput.isValid()) {
            double numberValue = (Long) value;
            if (numberValue > 10000000) {
                FacesMessage message = new FacesMessage(
                        FacesMessage.SEVERITY_WARN,
                        "Salary requirements is too much",
                        "Salary requirements is too much");
                throw new ValidatorException(message);
            } else if (numberValue < 1000 || numberValue > 10000000) {
                FacesMessage message = new FacesMessage(
                        FacesMessage.SEVERITY_WARN,
                        "Years of experience is invalid",
                        "Years of experience is invalid");
                throw new ValidatorException(message);
            }
        }
    }

    public void validatePhoneNumber(
            FacesContext context,
            UIComponent componentToValidate,
            Object value)
            throws ValidatorException {

        UIInput uiInput = (UIInput) componentToValidate;
        if (uiInput.isValid()) {
            String phoneValue = (String) value;
            if (!phoneValue.isEmpty()) {
                if (!phoneValue.matches("\\d{10}")) {
                    FacesMessage message = new FacesMessage(
                            FacesMessage.SEVERITY_WARN,
                            "Phone number is invalid",
                            "Phone number is invalid");
                    throw new ValidatorException(message);
                }
            }
        }
    }

    public void validateDOB(
            FacesContext context,
            UIComponent componentToValidate,
            Object value)
            throws ValidatorException {
        Date date = new Date();
        int dnow = date.getYear();
        int dob = 0;
        UIInput uiInput = (UIInput) componentToValidate;
        if (uiInput.isValid()) {
            Date dobValue = (Date) value;
            dob = dobValue.getYear();
            if (dobValue.compareTo(Calendar.getInstance().getTime()) > 0) {
                FacesMessage message = new FacesMessage(
                        FacesMessage.SEVERITY_WARN,
                        "Date is invalid",
                        "Date is invalid");
                throw new ValidatorException(message);
            } else if (dnow - dob < 20) {
                FacesMessage message = new FacesMessage(
                        FacesMessage.SEVERITY_WARN,
                        "Date is invalid Age > 20",
                        "Date is invalid Age > 20");
                throw new ValidatorException(message);
            }
        }
    }
    private int Start;

    public ApplicantFacade getApplicantFacade() {
        return applicantFacade;
    }

    public void setApplicantFacade(ApplicantFacade applicantFacade) {
        this.applicantFacade = applicantFacade;
    }

    public Applicant getApplicant() {
        return applicant;
    }

    public void setApplicant(Applicant applicant) {
        this.applicant = applicant;
    }

    public int getStart() {
        return Start;
    }

    public void setStart(int Start) {
        this.Start = Start;
    }
    public void validatedatestart(
            FacesContext context,
            UIComponent componentToValidate,
            Object start)
            throws ValidatorException {
        Date date = new Date();
        UIInput uiInput = (UIInput) componentToValidate;
        if (uiInput.isValid()) {
            Date dobValue = (Date) start;
            if (dobValue.compareTo(Calendar.getInstance().getTime()) < 0) {
                FacesMessage message = new FacesMessage(
                        FacesMessage.SEVERITY_WARN,
                        "Date is invalid",
                        "Date is invalid");
                throw new ValidatorException(message);
            }
            this.Start = dobValue.getDate();
    }
    }
    public void validatedateend(
            FacesContext context,
            UIComponent componentToValidate, Object end)
            throws ValidatorException {
        Date date = new Date();
        UIInput uiInput = (UIInput) componentToValidate;
        if (uiInput.isValid()) {
            Date dobValue = (Date) end;
            if (dobValue.getDate() < this.Start ) {
                FacesMessage message = new FacesMessage(
                        FacesMessage.SEVERITY_WARN,
                        "Ended Time < Start Time",
                        "Ended Time < Start Time");
                throw new ValidatorException(message);
            }
        }
    }
     public void validatespecialcharacters(
            FacesContext context,
            UIComponent componentToValidate, Object value)
            throws ValidatorException {
      
            String specialValue = (String) value;
            Pattern p = Pattern.compile("^[a-zA-Z0-9]*");

        //Match the given string with the pattern
        Matcher m = p.matcher(specialValue);

        //Check whether match is found
        boolean matchFound = m.matches();
                if (!matchFound) {
                    FacesMessage message = new FacesMessage(
                            FacesMessage.SEVERITY_WARN,
                            "special is invalid",
                            "special is invalid");
                    throw new ValidatorException(message);
                }
            }      
} 